

import java.util.Random;
import java.util.Scanner;

/**
 * 50道100以内加减法口算习题程序
 * 学号：202331108
 * 姓名：刘岩斌
 */
public class Lab_01 {
    public static void main(String[] args) {
        // 创建随机数生成器
        Random random = new Random();
        Scanner scanner = new Scanner(System.in);

        int correctCount = 0; // 正确答案计数
        int totalQuestions = 50; // 总题目数

        System.out.println("========== 100以内加减法口算习题 ==========");
        System.out.println("共 " + totalQuestions + " 道题目，请认真计算！");
        System.out.println("=========================================");

        // 生成50道题目
        for (int i = 1; i <= totalQuestions; i++) {
            // 生成两个100以内的随机数
            int num1 = random.nextInt(100) + 1;
            int num2 = random.nextInt(100) + 1;

            // 随机决定是加法还是减法
            boolean isAddition = random.nextBoolean();
            int correctAnswer;
            String operator;

            if (isAddition) {
                // 如果是加法，确保和在100以内
                while (num1 + num2 > 100) {
                    num1 = random.nextInt(100) + 1;
                    num2 = random.nextInt(100) + 1;
                }
                correctAnswer = num1 + num2;
                operator = "+";
            } else {
                // 如果是减法，确保结果为正数
                if (num1 < num2) {
                    int temp = num1;
                    num1 = num2;
                    num2 = temp;
                }
                correctAnswer = num1 - num2;
                operator = "-";
            }

            // 显示题目
            System.out.print("第 " + i + " 题: " + num1 + " " + operator + " " + num2 + " = ");

            // 获取用户输入
            int userAnswer;
            try {
                userAnswer = scanner.nextInt();
            } catch (Exception e) {
                System.out.println("请输入有效的数字！");
                scanner.next(); // 清除错误的输入
                i--; // 重新回答这道题
                continue;
            }

            // 检查答案
            if (userAnswer == correctAnswer) {
                System.out.println("✓ 正确！");
                correctCount++;
            } else {
                System.out.println("✗ 错误！正确答案是: " + correctAnswer);
            }
            System.out.println(); // 空行分隔
        }

        // 显示最终结果
        System.out.println("=========================================");
        System.out.println("答题完成！");
        System.out.println("总题数: " + totalQuestions);
        System.out.println("答对题数: " + correctCount);
        System.out.println("正确率: " + (correctCount * 100 / totalQuestions) + "%");

        scanner.close();
    }
}
    